package com.hibase.upms.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdNullEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.upms.constant.OrgManagerConstant;
import com.hibase.upms.entity.organization.MdsOrganization;
import com.hibase.upms.entity.organization.vo.MdsOrgChooseVO;
import com.hibase.upms.entity.organization.vo.MdsOrgInsertVO;
import com.hibase.upms.entity.organization.vo.MdsOrgSearchVO;
import com.hibase.upms.entity.organization.vo.MdsOrgUpdateVO;
import com.hibase.upms.entity.organization.vo.MdsOrgVO;
import com.hibase.upms.entity.organization.vo.MdsOrgViewVO;
import com.hibase.upms.service.organization.IMdsOrganizationService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChrisCui
 * @since 2019-04-17
 */
@RestController
@RequestMapping("/mds-organization")
@Api(tags = "组织管理")
public class MdsOrganizationController {

    @Autowired
    private IMdsOrganizationService orgService;

    @ApiOperation(value = "新建组织", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/addOrgInfo")
    public ResponseModel insertOrg(@RequestBody MdsOrgInsertVO orgInsertVO) {

        MdsOrganization mdsOrg = EntityUtil.transform(orgInsertVO, MdsOrganization.class);
        if(StrUtil.isNotBlank(orgInsertVO.getParentId())) {
        	MdsOrganization byId = orgService.getById(orgInsertVO.getParentId());
        	mdsOrg.setParentName(byId.getName());
        }
        orgService.changeUser(mdsOrg);
        //此处需要返回对象信息
        return ResponseModel.successMsg(mdsOrg.getId());
    }

    @ApiOperation(value = "更新组织", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updateOrgInfo")
    public ResponseModel updateOrg(@RequestBody MdsOrgUpdateVO orgUpdateVO) {

        MdsOrganization mdsOrg = EntityUtil.transform(orgUpdateVO, MdsOrganization.class);

        orgService.changeUser(mdsOrg);
        //此处需要返回对象信息
        return ResponseModel.successMsg(mdsOrg.getId());
    }

    @ApiOperation(value = "启用组织", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/enableOrgInfo")
    public ResponseModel enableOrg(@RequestBody OperationIdsEntity orgIdsVO) {

        orgService.changeOrgStatus(orgIdsVO.getIds(), OrgManagerConstant.RECORD_STATUS_ENABLE);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "停用组织", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/disableOrgInfo")
    public ResponseModel disableOrg(@RequestBody OperationIdsEntity orgIdsVO) {

        orgService.changeOrgStatus(orgIdsVO.getIds(), OrgManagerConstant.RECORD_STATUS_DISABLED);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询组织列表", httpMethod = "POST", response = MdsOrgVO.class)
    @HibasePostRequestMapping(value = "selectList")
    public ResponseModel selectList(@RequestBody MdsOrgSearchVO searchVO) {

        PageInfo result = orgService.selectList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), MdsOrgVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "查询组织详情", httpMethod = "POST", response = MdsOrgViewVO.class)
    @HibasePostRequestMapping(value = "viewDetails")
    public ResponseModel viewDetails(@RequestBody OperationIdEntity idEntity) {

        MdsOrganization mdsOrg = orgService.getById(idEntity.getId());

        MdsOrgViewVO mdsOrgViewVO = EntityUtil.transform(mdsOrg, MdsOrgViewVO.class);
        return ResponseModel.successMsg(mdsOrgViewVO);
    }

    @ApiOperation(value = "根据id值查询组织，用于选择框，查询第一级传入空", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectAreaById")
    public ResponseModel<MdsOrgChooseVO> selectAreaById(@RequestBody OperationIdNullEntity areaSearchVO) {

        QueryWrapper<MdsOrganization> wrapper = new QueryWrapper<>();

        if (StrUtil.isBlank(areaSearchVO.getId())) {

            wrapper.isNull("parent_id");
        } else {

            wrapper.eq("parent_id", areaSearchVO.getId());
        }

        List<MdsOrganization> result = orgService.list(wrapper);
        result.forEach(p->p.setFullName(p.getFullName().replace("海晨集团/","")));

        return ResponseModel.successMsg(EntityUtil.transform(result, MdsOrgChooseVO.class));
    }

    @ApiOperation(value = "根据组织层级，第一级id传入为空", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectFirstOrg")
    public ResponseModel<MdsOrgVO> selectFirstOrg(@RequestBody OperationIdNullEntity idEntity) {

        QueryWrapper<MdsOrganization> wrapper = new QueryWrapper<>();

        if (StrUtil.isBlank(idEntity.getId())) {

            wrapper.isNull("parent_id");
        } else {

            wrapper.eq("parent_id", idEntity.getId());
        }

        List<MdsOrganization> result = orgService.list(wrapper);

        return ResponseModel.successMsg(EntityUtil.transform(result, MdsOrgVO.class));
    }

    @ApiOperation(value = "查询当前用户下级组织", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectChildOrg")
    public ResponseModel<MdsOrgVO> selectChildOrg(@RequestBody OperationIdEntity idEntity) {

        PageInfo result = orgService.selectChildOrg(idEntity);
        result.setList(EntityUtil.transform(result.getList(), MdsOrgVO.class));
        return ResponseModel.successMsg(result);
    }
}
